using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace yW5Lib
{
	public class clsLocations : clsGenericBase<clsLocation>
	{
		public delegate void SavedEventHandler(int ID);

		private clsXml cXml;

		[CompilerGenerated]
		private SavedEventHandler SavedEvent;

		public event SavedEventHandler Saved
		{
			[CompilerGenerated]
			add
			{
				SavedEventHandler savedEventHandler = SavedEvent;
				SavedEventHandler savedEventHandler2;
				do
				{
					savedEventHandler2 = savedEventHandler;
					SavedEventHandler value2 = (SavedEventHandler)Delegate.Combine(savedEventHandler2, value);
					savedEventHandler = Interlocked.CompareExchange(ref SavedEvent, value2, savedEventHandler2);
				}
				while ((object)savedEventHandler != savedEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				SavedEventHandler savedEventHandler = SavedEvent;
				SavedEventHandler savedEventHandler2;
				do
				{
					savedEventHandler2 = savedEventHandler;
					SavedEventHandler value2 = (SavedEventHandler)Delegate.Remove(savedEventHandler2, value);
					savedEventHandler = Interlocked.CompareExchange(ref SavedEvent, value2, savedEventHandler2);
				}
				while ((object)savedEventHandler != savedEventHandler2);
			}
		}

		public clsLocations()
		{
			cXml = new clsXml();
		}

		public clsLocation LocationByID(int ID)
		{
			return base.get_ByID(ID);
		}

		public clsLocation LocationByIndex(int Index)
		{
			return base.get_ByIndex(Index);
		}

		public bool AddLocation(ref clsLocation Location)
		{
			bool result;
			if (Added(ref Location))
			{
				Location.Saved += Location_Saved;
				result = true;
				Location.AddedToProject = true;
			}
			else
			{
				result = false;
			}
			return result;
		}

		public void RemoveLocationByID(int ID)
		{
			if (IDUsed(ID))
			{
				LocationByID(ID).Saved -= Location_Saved;
				RemoveByID(ID);
			}
			else
			{
				clsShared.cLog.AddAction("Tried to remove location ID " + Conversions.ToString(ID) + " but it's not in the project.");
			}
		}

		public void RemoveLocationByIndex(int Index)
		{
			try
			{
				LocationByIndex(Index).Saved -= Location_Saved;
				RemoveByIndex(Index);
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		public string SelectedLocationsAsXML(clsCollection<int> colIDS)
		{
			string result = "";
			StringBuilder stringBuilder = new StringBuilder();
			try
			{
				stringBuilder.AppendLine("\t<LOCATIONS>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsLocation clsLocation = (clsLocation)enumerator.Current;
						if (colIDS.Contains(Conversions.ToString(clsLocation.ID)))
						{
							stringBuilder.AppendLine(clsLocation.DataAsXML());
						}
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				stringBuilder.AppendLine("\t</LOCATIONS>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool WriteXML(ref StringBuilder SBP)
		{
			bool result = true;
			try
			{
				SBP.AppendLine("\t<LOCATIONS>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsLocation clsLocation = (clsLocation)enumerator.Current;
						SBP.AppendLine(clsLocation.DataAsXML());
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				SBP.AppendLine("\t</LOCATIONS>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool CreateFromXML(string XMLData)
		{
			Clear();
			return UpdateFromXML(XMLData, ResetID: false, TriggerSaveEvent: false);
		}

		public bool UpdateFromXML(string XMLData, bool ResetID, bool TriggerSaveEvent)
		{
			bool result = true;
			try
			{
				string[] DataArray = new string[2];
				cXml.ExtractXMLArray(XMLData, "<LOCATIONS>", "</LOCATIONS>", "</LOCATION>", ref DataArray);
				int num = DataArray.Length - 1;
				for (int i = 0; i <= num; i++)
				{
					if (Strings.InStr(1, DataArray[i], "<LOCATION>", CompareMethod.Text) > 0)
					{
						clsLocation Location = new clsLocation();
						Location.CreateFromXML(DataArray[i]);
						if (Location.SortOrder == 0)
						{
							Location.SortOrder = i + 1;
						}
						if (ResetID)
						{
							Location.ID = 0;
							Location.SortOrder = base.Count + 1;
						}
						if (!AddLocation(ref Location))
						{
							clsShared.cLog.AddAction("Unable to add location " + Location.Title + " to project.");
							return false;
						}
						if (TriggerSaveEvent)
						{
							Location.TriggerSaveEvent();
						}
					}
				}
				DataArray = null;
				XMLData = "";
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				result = false;
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool AlphaSort()
		{
			bool result = false;
			try
			{
				bool flag;
				int i;
				do
				{
					flag = true;
					int num = base.Count - 1;
					for (i = 1; i <= num; i++)
					{
						clsLocation clsLocation = base.get_ByIndex(i);
						clsLocation cObject = base.get_ByIndex(i + 1);
						if (Operators.CompareString(cObject.Title, clsLocation.Title, TextCompare: false) < 0)
						{
							flag = false;
							RemoveByID(cObject.ID);
							if (!Inserted(ref cObject, i))
							{
								AddLocation(ref cObject);
								clsShared.cLog.AddAction("Error while sorting Locs - could not put " + cObject.Title + " back.");
								return result;
							}
						}
					}
				}
				while (!flag);
				i = 0;
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsLocation clsLocation = (clsLocation)enumerator.Current;
						i = (clsLocation.SortOrder = i + 1);
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public void Resort()
		{
			bool flag;
			int i;
			do
			{
				flag = true;
				int num = base.Count - 1;
				for (i = 1; i <= num; i++)
				{
					clsLocation clsLocation = base.get_ByIndex(i);
					clsLocation cObject = base.get_ByIndex(i + 1);
					if (cObject.SortOrder < clsLocation.SortOrder)
					{
						flag = false;
						RemoveByID(cObject.ID);
						if (!Inserted(ref cObject, i))
						{
							AddLocation(ref cObject);
							clsShared.cLog.AddAction("Error while sorting Locations - could not put " + cObject.Title + " back.");
							return;
						}
					}
				}
			}
			while (!flag);
			i = 0;
			IEnumerator enumerator = default(IEnumerator);
			try
			{
				enumerator = base.colObjects.GetEnumerator();
				while (enumerator.MoveNext())
				{
					clsLocation clsLocation = (clsLocation)enumerator.Current;
					i = (clsLocation.SortOrder = i + 1);
				}
			}
			finally
			{
				if (enumerator is IDisposable)
				{
					(enumerator as IDisposable).Dispose();
				}
			}
		}

		private void Location_Saved(int ID)
		{
			SavedEvent?.Invoke(ID);
		}
	}
}
